package com.example.koizzi.ad20180426_weather;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


import java.util.List;

/**
 * Created by KOIzzi on 2018/5/1.
 */

public class TempView extends View {
    private Paint paint;
    private Temprature[] tempratures;
    private int[] hx = new int[5];//高温x
    private int[] hy = new int[5];//高温y
    private int[] hyy = new int[5];//高温温度
    private int[] lx = new int[5];//低温x
    private int[] ly = new int[5];//低温y
    private int[] lyy = new int[5];//低温温度
    public TempView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        Path highPath = new Path();
        for(int i=0; i<hx.length;i++){
            if(i==0){
                highPath.moveTo(hx[i],hy[i]);
                canvas.drawText("高:"+String.valueOf(hyy[i])+"℃",hx[i]-70,hy[i]-30, paintText(2));
            }else {
                canvas.drawText(String.valueOf(hyy[i])+"℃",hx[i]-50,hy[i]-30, paintText(2));
                highPath.lineTo(hx[i],hy[i]);
            }
            paint.setStrokeWidth(10);
            canvas.drawPoint(hx[i],hy[i], paint);
        }
        paint.setStrokeWidth(5);
        canvas.drawPath(highPath,paint);
        Path lowPath = new Path();
        for(int i=0; i<lx.length;i++){
            if(i==0){
                canvas.drawText("低:"+String.valueOf(lyy[i])+"℃",lx[i]-70,ly[i]-30, paintText(2));
                lowPath.moveTo(lx[i], ly[i]);
            }else {
                canvas.drawText(String.valueOf(lyy[i])+"℃",lx[i]-50,ly[i]-30, paintText(2));
                lowPath.lineTo(lx[i], ly[i]);
            }
            paint.setStrokeWidth(10);
            canvas.drawPoint(lx[i], ly[i], paint);
        }
        paint.setStrokeWidth(5);
        canvas.drawPath(lowPath,paint);
    }

    private Paint paintText(int ints) {
        Paint Tpaint = new Paint();
        Tpaint.setTextSize(36);
        Tpaint.setColor(Color.BLUE);
        paint.setStrokeWidth(ints);
        return Tpaint;
    }

    public void setData(List<Weather.DataBean.ForecastBean> forecast, int width, int height) {
        tempratures = new Temprature[5];

        for (int i=0;i<tempratures.length;i++){
        tempratures[i] = new Temprature(Integer.parseInt(forecast.get(i).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(i).getLow().substring(3,5)));
            hyy[i] = tempratures[i].high;
            lyy[i] = tempratures[i].low;
            hx[i] = parseX(width,i);
            hy[i] = (parseY(height,hyy[i]));
            lx[i] = parseX(width,i);
            ly[i] = (parseY(height,lyy[i]));
        }
        invalidate();
    }

    private int parseY(int height, int str) {
        return (50 - str) * 20-height/4;
    }

    private int parseX(int width, int i) {
        return width/5/2 + i * width/5;
    }
}
